home *** CD-ROM | disk | FTP | other *** search
/ Aminet 20 / Aminet 20 (1997)(GTI - Schatztruhe)[!][Aug 1997].iso / Aminet / comm / www / tw2html.lha / tw2html-1.0 / tw2html.c < prev   
C/C++ Source or Header  |  1997-06-13  |  6KB  |  262 lines

  1. /*  tw2html.c
  2.     (C)1997 Adrian O' Neill
  3.     Converts Transwrite files to HTML format
  4.     See readme.txt for more information
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. #define def_lineclip 70
  12. #define out_lineclip 70
  13. #define BUFSIZE 5120
  14. #define MaxFilename 250
  15.  
  16. enum align_type {
  17.     left,
  18.     right,
  19.     center
  20. };
  21.  
  22. int lineclip=def_lineclip;
  23. enum align_type alignment = left;
  24. int al_change = 0;
  25.  
  26. void OutputLine(char *str, FILE *fptr)
  27. {
  28.     char *end, *space;
  29.     static char buffer[out_lineclip+2];
  30.  
  31.     while (strlen(str) > out_lineclip)  {
  32.         strncpy(buffer, str, out_lineclip);
  33.         buffer[out_lineclip] = 0;
  34.         if (space = strrchr(buffer, ' '))  {
  35.             *space=0;
  36.             fprintf(fptr, "%s\15\12",buffer);
  37.             str = str + (space - buffer + 1);
  38.         } else {
  39.             fprintf(fptr, "%s", buffer);
  40.             str = str + def_lineclip;
  41.         }
  42.     }
  43.     fprintf(fptr, "%s\15\12", str);
  44. }
  45.  
  46.  
  47. void PrintLine(char *line, int line_len, FILE *fptr)
  48. {
  49.     if ((line_len < lineclip) && (!al_change))  {
  50.         OutputLine(line, fptr);
  51.         fprintf(fptr, "<br>\15\12");
  52.     } else {
  53.         al_change = 0;
  54.         OutputLine(line, fptr);
  55.         fprintf(fptr, "\15\12<p");
  56.         if (alignment != left)  {
  57.             fprintf(fptr, " align=");
  58.             switch (alignment)  {
  59.                 case right: fprintf(fptr, "right"); break;
  60.                 case center:  fprintf(fptr, "center"); break;
  61.             }
  62.         }
  63.         fprintf(fptr, ">\15\12");
  64.     }
  65. }
  66.  
  67. void ConvertLine(char *inline, char *outline)
  68. {
  69.     unsigned char i;
  70.     unsigned char ch[8], num[8];
  71.     int bold = 0, italic = 0, under = 0;
  72.     int j, count = 1;
  73.  
  74.     i = *inline;
  75.     *outline = 0;
  76.  
  77.     while (*inline++)  {
  78.         if ((i < 32) && (i >= 0))  {
  79.             ch[0]=0;
  80.             switch (i)  {
  81.                 case 2:
  82.                     if (!italic)  {
  83.                         italic = (count++);
  84.                         strcpy(ch,"<i>");
  85.                     }
  86.                     break;
  87.                 case 3:
  88.                     if (!bold)  {
  89.                         bold = (count++);
  90.                         strcpy(ch,"<b>");
  91.                     }
  92.                     break;
  93.                 case 4:
  94.                     if (!under)  {
  95.                         under = (count++);
  96.                         strcpy(ch,"<u>");
  97.                     }
  98.                     break;
  99.                 case 5:
  100.                     italic = 0;
  101.                     strcpy(ch,"</i>");
  102.                     break;
  103.                 case 6:
  104.                     bold = 0;
  105.                     strcpy(ch,"</b>");
  106.                     break;
  107.                 case 7:
  108.                     under = 0;
  109.                     strcpy(ch,"</u>");
  110.                     break;
  111.                 default : ch[0] = 0;
  112.             }
  113.         } else {
  114.             if ((i >= 32) && (i < 160))  {
  115.                 switch (i)  {
  116.                     case '<' : strcpy(ch,"<"); break;
  117.                     case '>' : strcpy(ch,">"); break;
  118.                     case '&' : strcpy(ch,"&"); break;
  119.                     default:
  120.                         ch[0] = i;
  121.                         ch[1] = 0;
  122.                         if (i >= 128)
  123.                             ch[0] = 0;
  124.                 }
  125.             } else {
  126.                 strcpy(ch, "&#");
  127.                 sprintf(num, "%i", i);
  128.                 strcat(ch, num);
  129.                 strcat(ch, ";");
  130.             }
  131.         }
  132.         strcat(outline, ch);
  133.         i = *inline;
  134.     }
  135.     for (j=count; j>=1; j--)  {
  136.         if (bold == j)
  137.             strcat(outline, "</b>");
  138.         if (italic == j)
  139.             strcat(outline, "</i>");
  140.         if (under == j)
  141.             strcat(outline, "</u>");
  142.     }
  143. }
  144.  
  145.  
  146. void ChangeAlignment(*str)
  147. {
  148.     if ((strncmp(str, "jl", 2) == 0) || (strncmp(str, "jf", 2) == 0))
  149.         alignment = left;
  150.     if (strncmp(str, "jr", 2) == 0)
  151.         alignment = right;
  152.     if (strncmp(str, "jc", 2) == 0)
  153.         alignment = center;
  154. }
  155.  
  156.  
  157. void ProcessAlign(char *str)
  158. {
  159.     do {
  160.         str++;
  161.         ChangeAlignment(str);
  162.     } while ((*str) && (str = strchr(str, ':')));
  163. }
  164.  
  165.  
  166. void ReadData(unsigned char *in, FILE *fin)
  167. {
  168.     int emptyline = 1;
  169.  
  170.     while ( (emptyline) && (fgets(in, BUFSIZE, fin)) )  {
  171.         emptyline = 0;
  172.         if ( (*in==0) || (*in==10) || (strspn(in," ")==strlen(in)) )  {
  173.             emptyline=1;
  174.         } else {
  175.             while ( (*in==167) || ((*in > 0) && (*in < 32)) )  {
  176.                 if (*in == 167)  {
  177.                     ProcessAlign(in);
  178.                     emptyline = 1;
  179.                     break;
  180.                 }
  181.                 in++;
  182.             }
  183.         }
  184.     }
  185. }
  186.  
  187.  
  188. int main(int argc, char *argv[])
  189. {
  190.     unsigned char *in1, *in2, *out;
  191.     FILE *fin, *fout;
  192.     char outfilename[MaxFilename + 5];
  193.     enum align_type first, second;
  194.  
  195.     if ((argc<2) || (argc>3))  {
  196.         printf("Usage: %s TWFile [OutputFile]\n", argv[0]);
  197.         exit(1);
  198.     }
  199.  
  200.     if ( !(in1 = (unsigned char *) malloc (BUFSIZE)) ||
  201.             !(in2 = (unsigned char *) malloc (BUFSIZE)) ||
  202.             !(out = (unsigned char *) malloc (6*BUFSIZE)) )  {
  203.         printf("Error: Not enough memory available.\n");
  204.         exit(1);
  205.     }
  206.  
  207.     if (!(fin=fopen(argv[1], "r")))  {
  208.         printf("Error: Can't open %s for reading.\n",argv[1]);
  209.         exit(1);
  210.     }
  211.  
  212.     if (strlen(argv[1]) > MaxFilename) {
  213.         printf("Error: Filename too big!\n");
  214.         exit(1);
  215.     }
  216.  
  217.     if (argc==2)  {
  218.         strcpy(outfilename, argv[1]);
  219.         strcat(outfilename, ".html");
  220.     } else {
  221.         strcpy(outfilename, argv[2]);
  222.     }
  223.  
  224.     if (!(fout=fopen(outfilename, "w")))  {
  225.         printf("Error: Can't open %s for writing.\n",outfilename);
  226.         exit(1);
  227.     }
  228.  
  229.     fprintf(fout, "<html>\15\12<head>\15\12<title>%s</title>\15\12",argv[1]);
  230.     fprintf(fout, "<meta name=\"Generator\" content=\"tw2html\">\15\12");
  231.     fprintf(fout, "</head>\15\12<body>\15\12");
  232.  
  233.     ReadData(in1, fin);
  234.  
  235.     if (alignment != left)  {
  236.         al_change = 1;
  237.         PrintLine("", 0, fout);
  238.     }
  239.  
  240.     while (!feof(fin))  {
  241.         first = alignment;
  242.         ReadData(in2, fin);
  243.         second = alignment;
  244.         if (first != second)
  245.             al_change = 1;
  246.         ConvertLine(in1, out);
  247.         PrintLine(out, strlen(in2), fout);
  248.         strcpy(in1, in2);
  249.     }
  250.  
  251.     fprintf(fout, "</body>\15\12</html>\15\12");
  252.     fclose(fin);
  253.     fclose(fout);
  254.     free(in1);
  255.     free(in2);
  256.     free(out);
  257.     return(0);
  258. }
  259.  
  260.  
  261.  
  262.